package com.zmj.sy.mom.srv.aps.controller;

import com.zmj.sy.mom.srv.aps.bean.vo.base.*;
import com.zmj.sy.mom.srv.aps.bean.vo.mbd.*;
import com.zmj.sy.mom.srv.aps.bean.vo.mbd.factory.MbdBalanceRateDataListOfFactoryResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.mbd.factory.MbdBalanceRateOfFactoryReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.mbd.factory.MbdBalanceRateOfFactoryResVo;
import com.zmj.sy.mom.srv.aps.bean.vo.mbd.group.*;
import com.zmj.sy.mom.srv.aps.bean.vo.mbd.plan.MbdPlanMonthlyReqVo;
import com.zmj.sy.mom.srv.aps.bean.vo.mbd.plan.MbdPlanMonthlyResVo;
import com.zmj.sy.mom.srv.aps.service.MbdService;
import com.zmj.sy.mom.srv.aps.utils.ControllerAspectIgnore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

@RequiredArgsConstructor
@Api(tags = "MBD")
@RestController
@RequestMapping("/mbd")
public class MbdController {

    private final MbdService mbdService;

    @ApiOperation("列表")
    @GetMapping("/list")
    public BasePageResVo<MbdListResVo> list(BasePageReqVo reqVo) {
        return mbdService.list(reqVo);
    }

    @ApiOperation("修改")
    @PostMapping("/edit")
    public BaseResVo edit(@RequestBody MbdEditReqVo reqVo){
        mbdService.edit(reqVo);
        return BaseResVo.ok();
    }


    @ApiOperation("获取平衡率配置")
    @GetMapping("/getBalanceConfig")
    public MbdGetBalanceRateConfigResVo getBalanceConfig(BaseIdReqVo reqVo){
        return mbdService.getBalanceConfig(reqVo);
    }
    @ApiOperation("修改平衡率配置")
    @PostMapping("/addBalanceConfig")
//    @BaseRepeatLock(lockKey = "#reqVo.orderId", lockTime = 5)/**/
    public BaseResVo addBalanceConfig(@RequestBody MbdAddBalanceRateConfigReqVo reqVo){
        mbdService.addBalanceConfig(reqVo);
        return BaseResVo.ok();
    }

    @ApiOperation("平衡率新增工序下拉列表")
    @GetMapping("/listBalanceRatePhase")
    public BaseListResVo<MbdBalancePhaseListResVo> listBalanceRatePhase(MbdBalancePhaseListReqVo reqVo){
        return new BaseListResVo<>(mbdService.listBalanceRatePhase(reqVo));
    }

    @ApiOperation("平衡率展示")
    @PostMapping("/balanceRate")
    public MbdBalanceRateResVo balanceRate(@RequestBody MbdBalanceRateReqVo reqVo){
        return mbdService.balanceRate(reqVo);
    }

    @ApiOperation("查询平衡率留痕列表")
    @GetMapping("/listBalanceRateData")
    public BaseListResVo<MbdBalanceRateDataListResVo> listBalanceRateData(MbdBalanceRateDataListReqVo reqVo){
        return new BaseListResVo<>(mbdService.listBalanceRateData(reqVo));
    }

    @ApiOperation("查询平衡率留痕")
    @GetMapping("/getBalanceRateData")
    public MbdBalanceRateGetDataResVo getBalanceRateData(BaseIdReqVo reqVo){
        return mbdService.getBalanceRateData(reqVo);
    }
    @ApiOperation("留痕平衡率")
    @PostMapping("/saveBalanceRateData")
    public BaseResVo saveBalanceRateData(@RequestBody MbdBalanceRateDataSaveReqVo reqVo){
         mbdService.saveBalanceRateData(reqVo);
        return BaseResVo.ok();
    }

    @ApiOperation("删除平衡率留痕")
    @PostMapping("/deleteBalanceRateData")
    public BaseResVo deleteBalanceRateData(@RequestBody BaseIdReqVo reqVo){
        mbdService.deleteBalanceRateData(reqVo);
        return BaseResVo.ok();
    }

    @ApiOperation("生产平衡率汇总对比")
    @GetMapping("/balanceRateComparison")
    public BaseListResVo<MbdBalanceRateDataComparisonResVo> balanceRateComparison(MbdBalanceRateDataComparisonReqVo reqVo){
        return new BaseListResVo<>(mbdService.balanceRateComparison(reqVo));
    }

    @ApiOperation("生产平衡率汇总对比导出")
    @GetMapping("/exportBalanceRateComparison")
    @ControllerAspectIgnore
    public ResponseEntity<byte[]> exportBalanceRateComparison(MbdBalanceRateDataComparisonReqVo reqVo) throws IOException {
        try (Workbook workbook = mbdService.exportBalanceRateComparison(reqVo)) {
            String fileName = "生产平衡率汇总对比.xlsx";
            String fileNameEncoded = new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            workbook.write(bytes);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentDisposition(ContentDisposition.builder("attachment").filename(fileNameEncoded).build());
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            return new ResponseEntity<>(bytes.toByteArray(), headers, HttpStatus.OK);
        }
    }
//
//    @ApiOperation("定时套料")
//    @GetMapping("/nestSync")
//    public BaseResVo nestSync(){
//        mbdService.nestSync();
//        return BaseResVo.ok();
//    }



    @ApiOperation("工作组平衡率--获取平衡率配置")
    @GetMapping("/getBalanceConfigOfGroup")
    public MbdGetBalanceRateConfigOfGroupResVo getBalanceConfigOfGroup(BaseIdReqVo reqVo){
        return mbdService.getBalanceConfigOfGroup(reqVo);
    }
    @ApiOperation("工作组平衡率--修改平衡率配置")
    @PostMapping("/addBalanceConfigOfGroup")
//    @BaseRepeatLock(lockKey = "#reqVo.orderId", lockTime = 5)
    public BaseResVo addBalanceConfig(@RequestBody MbdAddBalanceRateConfigOfGroupReqVo reqVo){
        mbdService.addBalanceConfigOfGroup(reqVo);
        return BaseResVo.ok();
    }

    @ApiOperation("工作组平衡率--平衡率新增工作组下拉列表")
    @GetMapping("/listBalanceRatePhaseOfGroup")
    public BaseListResVo<MbdBalanceGroupListResVo> listBalanceRateGroup(MbdBalanceGroupListReqVo reqVo){
        return new BaseListResVo<>(mbdService.listBalanceRateGroup(reqVo));
    }

    @ApiOperation("工作组平衡率--平衡率展示")
    @PostMapping("/balanceRateOfGroup")
    public MbdBalanceRateOfGroupResVo balanceRateOfGroup(@RequestBody MbdBalanceRateOfGroupReqVo reqVo){
        return mbdService.balanceRateOfGroup(reqVo);
    }

    @ApiOperation("工作组平衡率--查询平衡率留痕列表")
    @GetMapping("/listBalanceRateDataOfGroup")
    public BaseListResVo<MbdBalanceRateDataListResVo> listBalanceRateDataOfGroup(MbdBalanceRateDataListReqVo reqVo){
        return new BaseListResVo<>(mbdService.listBalanceRateDataOfGroup(reqVo));
    }

    @ApiOperation("工作组平衡率--查询平衡率留痕")
    @GetMapping("/getBalanceRateDataOfGroup")
    public MbdBalanceRateGetDataOfGroupResVo getBalanceRateDataOfGroup(BaseIdReqVo reqVo){
        return mbdService.getBalanceRateDataOfGroup(reqVo);
    }
    @ApiOperation("工作组平衡率--留痕平衡率")
    @PostMapping("/saveBalanceRateDataOfGroup")
    public BaseResVo saveBalanceRateData(@RequestBody MbdBalanceRateDataOfGroupSaveReqVo reqVo){
        mbdService.saveBalanceRateDataOfGroup(reqVo);
        return BaseResVo.ok();
    }

    @ApiOperation("工作组平衡率--删除平衡率留痕")
    @PostMapping("/deleteBalanceRateDataOfGroup")
    public BaseResVo deleteBalanceRateDataOfGroup(@RequestBody BaseIdReqVo reqVo){
        mbdService.deleteBalanceRateData(reqVo);
        return BaseResVo.ok();
    }

    @ApiOperation("工作组平衡率--生产平衡率汇总对比")
    @GetMapping("/balanceRateComparisonOfGroup")
    public BaseListResVo<MbdBalanceRateDataComparisonOfGroupResVo> balanceRateComparisonOfGroup(MbdBalanceRateDataComparisonOfGroupReqVo reqVo){
        return new BaseListResVo<>(mbdService.balanceRateComparisonOfGroup(reqVo));
    }

    @ApiOperation("工作组平衡率--生产平衡率汇总对比导出")
    @GetMapping("/exportBalanceRateComparisonOfGroup")
    @ControllerAspectIgnore
    public ResponseEntity<byte[]> exportBalanceRateComparisonOfGroup(MbdBalanceRateDataComparisonOfGroupReqVo reqVo) throws IOException {
        try (Workbook workbook = mbdService.exportBalanceRateComparisonOfGroup(reqVo)) {
            String fileName = "生产平衡率汇总对比.xlsx";
            String fileNameEncoded = new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            workbook.write(bytes);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentDisposition(ContentDisposition.builder("attachment").filename(fileNameEncoded).build());
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            return new ResponseEntity<>(bytes.toByteArray(), headers, HttpStatus.OK);
        }
    }

    @ApiOperation("获取MBD的三四五拼部件下拉框")
    @GetMapping("/select")
    public BaseSelectResVo select() {
        return mbdService.select();
    }

    @ApiOperation("全厂平衡率")
    @GetMapping("/balanceRateOfFactory")
    public MbdBalanceRateOfFactoryResVo balanceRateOfFactory(MbdBalanceRateOfFactoryReqVo reqVo) {
        return mbdService.balanceRateOfFactory(reqVo);
    }

    @ApiOperation("全厂平衡率--留痕列表查询")
    @GetMapping("/listBalanceRateDataOfFactory")
    public BaseListResVo<MbdBalanceRateDataListOfFactoryResVo> listBalanceRateDataOfFactory(BaseIdReqVo reqVo) {
        return new BaseListResVo<>(mbdService.listBalanceRateDataOfFactory(reqVo.getId()));
    }

    @ApiOperation("焊丝消耗平衡率")
    @GetMapping("/balanceRateOfRobotWeldingWire")
    public BaseListResVo<MbdBalanceRateOfRobotWeldingWireResVo> listRobotWeldingWireUsage(BaseIdReqVo reqVo) {
        List<MbdBalanceRateOfRobotWeldingWireResVo> weldingWireUsage = mbdService.listRobotWeldingWireUsage(reqVo.getId());
        return new BaseListResVo<>(weldingWireUsage);
    }

    @ApiOperation("月度策划总览")
    @GetMapping("/plan/monthly")
    public BaseListResVo<MbdPlanMonthlyResVo> monthly(MbdPlanMonthlyReqVo reqVo) {
        List<MbdPlanMonthlyResVo> data = mbdService.monthly(reqVo);
        return new BaseListResVo<>(data);
    }

}
